package unit;

import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import main.DieRoll;
import main.Hero;
import main.NumberSelecter;
import main.UI;
import main.Unit;

public class HobbitHero extends Hero {
	// Hobbits have the Resistant to Magic rule, which lets them resist with one dice even at 0 will	
	public boolean canResist() {
		return true;
	}
	
	public void resistMagic(int rollToBeat, Button resistSucceed, Button resistFail) {
		game.myUI.storeVisibleComponents();
		game.myUI.disableComponents();
		Button useWill = new Button("Resist");
		useWill.setName("resistButton");
		useWill.setBounds(game.myUI.dim.width-100, 0, 100, 100);
		
		Button noWill = new Button("Don't resist");
		noWill.setName("resistButton");
		noWill.setBounds(game.myUI.dim.width-100, 100, 100, 100);
		
		final Unit thisUnit = this;
		
		useWill.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// If we have will, choose how much to use
				if (willLeft > 0) {
					final NumberSelecter diceToResist = new NumberSelecter(1,willLeft,1,"Use how much will?", game.myUI, null);;
					
					Button codeToExecute = new Button();
					codeToExecute.addMouseListener(new MouseListener() {
						public void mouseClicked(MouseEvent arg0) {
							ArrayList<DieRoll> willRolls = new ArrayList<DieRoll>();
							boolean isModifiable = false;
							for (int i = 0; i < diceToResist.getNumber(); i++) {
								DieRoll willRoll = new DieRoll(thisUnit, game.myUI.dim.width-200, game.myUI.dim.height/2-30*(i+1), rollToBeat+"+ to resist", game.myUI);
								willRolls.add(willRoll);
								if (willRoll.isModifiable())
									isModifiable = true;
							}
							useWill(diceToResist.getNumber());
							if (!isModifiable) {
								UI.messageLog.add(toChoiceString() + " rolling to resist: " + willRolls);
								int willNum = 0;
								for (DieRoll dr : willRolls) {
									willNum = Math.max(willNum, dr.getRoll());
									dr.cleanup();
								}
								game.myUI.removeByName("resistButton");
								if (willNum < rollToBeat) {
									game.myUI.reenableComponents();
									if (resistFail != null)
										resistFail.dispatchEvent(new MouseEvent(resistFail, MouseEvent.MOUSE_CLICKED, 
												System.currentTimeMillis(), 0, 10, 10, 1, false));
								}
								else {
									game.myUI.reenableComponents();
									if (resistSucceed != null)
										resistSucceed.dispatchEvent(new MouseEvent(resistSucceed, MouseEvent.MOUSE_CLICKED, 
												System.currentTimeMillis(), 0, 10, 10, 1, false));
								}
								game.myUI.repaint();
							}
							else {
								System.out.println("Is modifiable");
								System.out.println(game.myUI.endPhase.isVisible());
								useWill.setEnabled(false);
								noWill.setEnabled(false);
								
								ActionListener mightDone = new ActionListener() {
									public void actionPerformed(ActionEvent e) {
										int willNum = 0;
										for (DieRoll dr : willRolls) {
											willNum = Math.max(willNum, dr.getRoll());
											dr.cleanup();
										}
										UI.messageLog.add(toChoiceString() + " rolling to resist: " + willRolls);
										game.myUI.removeByName("resistButton");
										game.myUI.disableMight();
										if (willNum < rollToBeat) {
											game.myUI.reenableComponents();
											if (resistFail != null)
												resistFail.dispatchEvent(new MouseEvent(resistFail, MouseEvent.MOUSE_CLICKED, 
														System.currentTimeMillis(), 0, 10, 10, 1, false));
										}
										else {
											game.myUI.reenableComponents();
											if (resistSucceed != null)
												resistSucceed.dispatchEvent(new MouseEvent(resistSucceed, MouseEvent.MOUSE_CLICKED, 
														System.currentTimeMillis(), 0, 10, 10, 1, false));
										}
										game.myUI.repaint();
									}
								};
								game.myUI.enableMight(mightDone);
							}
						}
						public void mouseEntered(MouseEvent arg0) {}
						public void mouseExited(MouseEvent arg0) {}
						public void mousePressed(MouseEvent arg0) {}
						public void mouseReleased(MouseEvent arg0) {}
					});
					diceToResist.setCodeToExecute(codeToExecute);
				}
				// Otherwise, just use 1
				else {
					DieRoll willRoll = new DieRoll(thisUnit, game.myUI.dim.width-200, game.myUI.dim.height/2-30, rollToBeat+"+ to resist", game.myUI);
					if (!willRoll.isModifiable()) {
						UI.messageLog.add(toChoiceString() + " rolling to resist: " + willRoll);
						int willNum = willRoll.getRoll();
						willRoll.cleanup();
						game.myUI.removeByName("resistButton");
						if (willNum < rollToBeat) {
							game.myUI.reenableComponents();
							if (resistFail != null)
								resistFail.dispatchEvent(new MouseEvent(resistFail, MouseEvent.MOUSE_CLICKED, 
										System.currentTimeMillis(), 0, 10, 10, 1, false));
						}
						else {
							game.myUI.reenableComponents();
							if (resistSucceed != null)
								resistSucceed.dispatchEvent(new MouseEvent(resistSucceed, MouseEvent.MOUSE_CLICKED, 
										System.currentTimeMillis(), 0, 10, 10, 1, false));
						}
						game.myUI.repaint();
					}
					else {
						System.out.println("Is modifiable");
						System.out.println(game.myUI.endPhase.isVisible());
						useWill.setEnabled(false);
						noWill.setEnabled(false);
						
						ActionListener mightDone = new ActionListener() {
							public void actionPerformed(ActionEvent e) {
								UI.messageLog.add(toChoiceString() + " rolling to resist: " + willRoll);
								int willNum = willRoll.getRoll();
								willRoll.cleanup();
								game.myUI.removeByName("resistButton");
								game.myUI.disableMight();
								if (willNum < rollToBeat) {
									game.myUI.reenableComponents();
									if (resistFail != null)
										resistFail.dispatchEvent(new MouseEvent(resistFail, MouseEvent.MOUSE_CLICKED, 
												System.currentTimeMillis(), 0, 10, 10, 1, false));
								}
								else {
									game.myUI.reenableComponents();
									if (resistSucceed != null)
										resistSucceed.dispatchEvent(new MouseEvent(resistSucceed, MouseEvent.MOUSE_CLICKED, 
												System.currentTimeMillis(), 0, 10, 10, 1, false));
								}
								game.myUI.repaint();
							}
						};
						game.myUI.enableMight(mightDone);
					}
				}
			}
		});
		
		noWill.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				game.myUI.removeByName("resistButton");
				game.myUI.reenableComponents();
				if (resistFail != null)
					resistFail.dispatchEvent(new MouseEvent(resistFail, MouseEvent.MOUSE_CLICKED, 
							System.currentTimeMillis(), 0, 10, 10, 1, false));
				game.myUI.repaint();
			}
		});
		
		game.myUI.add(useWill);
		game.myUI.add(noWill);
	}
}
